<template>
  <el-container class="login-container">
    <el-main>
      <el-row justify="center" align="middle" class="hero-section">
        <el-col :span="24" class="text-center">
          <h1 class="main-title">欢迎使用简历生成器</h1>
          <el-button 
            type="primary" 
            size="large" 
            class="start-button"
            @click="showAuthDialog = true">
            立即开始
          </el-button>
        </el-col>
      </el-row>

      <!-- 登录/注册弹窗 -->
      <el-dialog 
        v-model="showAuthDialog" 
        :before-close="handleClose"
        class="auth-dialog"
        style="border-radius: 20px;
        padding: 40px;">
        <div class="form-box login" v-if="activeTab === 'login'">
          <form @submit.prevent="handleLogin">
            <h2>登录</h2>
            <div class="input-box">
              <span class="icon"><ion-icon name="person"></ion-icon></span>
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入账号"
                required />
            </div>
            <div class="input-box">
              <span class="icon"><ion-icon name="lock-closed"></ion-icon></span>
              <el-input 
                v-model="loginForm.password" 
                type="password"
                placeholder="请输入密码"
                required />
            </div>
            <div class="remember-forget">
              <label>
                <input type="checkbox">记住我
              </label>
              <a href="#">忘记密码？</a>
            </div>
            <el-button type="primary" native-type="submit" class="submit-btn">
              登录
            </el-button>
            <div class="login-register">
              <p>没有账号？
                <a href="#" @click.prevent="activeTab = 'register'">立即注册</a>
              </p>
            </div>
          </form>
        </div>

        <div class="form-box register" v-if="activeTab === 'register'">
          <form @submit.prevent="handleRegister">
            <h2>注册</h2>
            <div class="input-box">
              <span class="icon"><ion-icon name="person"></ion-icon></span>
              <el-input 
                v-model="registerForm.username" 
                placeholder="请输入账号"
                required />
            </div>
            <div class="input-box">
              <span class="icon"><ion-icon name="lock-closed"></ion-icon></span>
              <el-input 
                v-model="registerForm.password" 
                type="password"
                placeholder="请输入密码"
                required />
            </div>
            <el-button type="primary" native-type="submit" class="submit-btn">
              注册
            </el-button>
            <div class="login-register">
              <p>已有账号？
                <a href="#" @click.prevent="activeTab = 'login'">立即登录</a>
              </p>
            </div>
          </form>
        </div>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const showAuthDialog = ref(false)
const activeTab = ref('login')

const loginForm = ref({
  username: '',
  password: ''
})

const registerForm = ref({
  username: '',
  password: ''
})

const handleClose = (done) => {
  ElMessage.info('请先登录或注册')
  done()
}

const handleLogin = () => {
  if (loginForm.value.username && loginForm.value.password) {
    router.push({
      path: '/home',
      query: {
        username: loginForm.value.username
      }
    })
  } else {
    ElMessage.error('请输入用户名和密码')
  }
}

const handleRegister = () => {
  if (registerForm.value.username && registerForm.value.password) {
    ElMessage.success('注册成功，请登录')
    activeTab.value = 'login'
    loginForm.value.username = registerForm.value.username
    loginForm.value.password = registerForm.value.password
  } else {
    ElMessage.error('请输入用户名和密码')
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
}

.hero-section {
  padding: 80px 20px;
}

.main-title {
  font-size: 3.5rem;
  color: #0288D1;
  margin-bottom: 40px;
  font-weight: 800;
  text-shadow: 0px 0px 4px #7db0e3;
}

.start-button {
  background-color: #1976D2;
  border: none;
  padding: 12px 36px;
  font-size: 1.1rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.start-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 登录注册弹窗样式 */
.auth-dialog {
  border-radius: 20px;
}

.form-box {
  width: 100%;
}

.form-box h2 {
  font-size: 2rem;
  color: #1976D2;
  text-align: center;
  margin: 10px 0 30px 0;
}

.input-box {
  position: relative;
  margin-bottom: 30px;
  font-size: 20px;
  
}

.input-box .icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #1976D2;
  font-size: 1.2rem;
}

.input-box .el-input {
  width: 100%;
  /* font-size: 18px; */
  /* padding-left: 40px; */
}

.remember-forget {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.remember-forget label {
  color: #666;
}

.remember-forget a {
  color: #1976D2;
  text-decoration: none;
}

.remember-forget a:hover {
  text-decoration: underline;
}

.submit-btn {
  width: 100%;
  margin-bottom: 20px;
}

.login-register {
  text-align: center;
  color: #666;
}

.login-register a {
  color: #1976D2;
  text-decoration: none;
}

.login-register a:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .form-box {
    padding: 20px;
  }
  
  .form-box h2 {
    font-size: 1.5rem;
  }
}
</style>
